﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
using VM.Common.DataUntil;
using VM.Common.EntityEnum;
using VM.Common.Result;
using VM.Entity.EntityDto.common;
using VM.Entity.EntityDto.Topic;
using VM.Entity.EntityMap.Member;
using VM.Entity.EntityMap.Topic;
using VM.IBLL.Member;
using VM.IBLL.Token;
using VM.IBLL.Topic;
using VM.WebCore.Jwt;
using VM.WebCore.SwaggerExtend;
using VM.Wechat;

namespace VM.WebAPI.Controllers
{

    /// <summary>
    /// 话题相关控制类
    /// </summary>
    [Route("api/topic")]
    [ApiController]
    public class TopicController : ControllerBase
    {
        private readonly ITopicBLL _ITopicBLL;
        private readonly IJWTService _IJWTService;
        private readonly IHttpContextAccessor _HttpContextAccessor;


        public TopicController(IJWTService iJWTService, ITopicBLL iTopicBLL, IHttpContextAccessor httpContextAccessor)
        {

            _ITopicBLL = iTopicBLL;
            _IJWTService = iJWTService;
            _HttpContextAccessor = httpContextAccessor;
        }


        /// <summary>
        /// 添加话题类别 PC
        /// </summary>
        /// <param name="sys_topicType">话题类别信息</param>
        /// <returns></returns>
        [HttpPost]
        [Authorize]//要求通过授权才行---只要能鉴权成功，都能通过]
        [Route("addTopicType")]
        [ApiExplorerSettings(IgnoreApi = false, GroupName = nameof(ApiVersions.V1))]
        public ApiDataResult<saveCommonRes> addTopicType(addtopicTypeDto obj)
        {

            //获取到header 内的用户信息进行解析
            var authorizationHeader = _HttpContextAccessor.HttpContext.Request.Headers["Authorization"];
            //解析jwt
            string tokenInfo = authorizationHeader.ToString().Replace("bearer ", "").Replace("Bearer ", "");
            var AESId = _IJWTService.AnaliseToken(tokenInfo, "id");
            //解析userid
            int  UserId =Convert.ToInt32( AESUntil.Decrypt(AESId));
            obj.createUserId = UserId;
            obj.updateUserId = UserId;
            obj.createDate= DateTime.Now;
            obj.updateDate = DateTime.Now;
            obj.billStatus = 1;
            


            return null;// _ITopicBLL.addTopicType(obj);

        }



        /// <summary>
        /// 获取话题类别列表，手机端pageSize尽可能大一些，pc端按照页面高度适量传入   PC+PHONE
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [Authorize]//要求通过授权才行---只要能鉴权成功，都能通过]
        [Route("getTopicTypeList")]
        [ApiExplorerSettings(IgnoreApi = false, GroupName = nameof(ApiVersions.V1))]
        public ApiDataResult<PagingData<getTopicTypeListDto>> getTopicTypeList(int pageIndex = 1, int pageSize = 100)
        {
            //校验验证码是否可用，如果可用，返回true
            ApiDataResult<PagingData<getTopicTypeListDto>> Res = _ITopicBLL.getTopicTypeList(pageSize, pageIndex);
            return Res;
        }


        /// <summary>
        /// 添加话题 PC+PHONE(现在手机端先不添加)
        /// </summary>
        /// <param name="sys_topic">话题信息</param>
        /// <returns></returns>
        [HttpPost]
        [Authorize]//要求通过授权才行---只要能鉴权成功，都能通过]
        [Route("addTopic")]
        [ApiExplorerSettings(IgnoreApi = false, GroupName = nameof(ApiVersions.V1))]
        public ApiResult addTopic(sys_topic obj)
        {
            //获取到header 内的用户信息进行解析
            var authorizationHeader = _HttpContextAccessor.HttpContext.Request.Headers["Authorization"];
            //解析jwt
            string tokenInfo = authorizationHeader.ToString().Replace("bearer ", "").Replace("Bearer ", "");
            var AESId = _IJWTService.AnaliseToken(tokenInfo, "id");
            //解析userid
            int  UserId =Convert.ToInt32( AESUntil.Decrypt(AESId));
            obj.createUserId=UserId;
            obj.billStatus = 1;
            obj.updateUserId=UserId;
            obj.createDate = DateTime.Now;
            obj.updateDate = DateTime.Now;
            obj.useQty = 0;

            return _ITopicBLL.addTopic(obj);

        }
        /// <summary>
        /// 获取话题列表，   PC+PHONE
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [Authorize]//要求通过授权才行---只要能鉴权成功，都能通过]
        [Route("getTopicList")]
        [ApiExplorerSettings(IgnoreApi = false, GroupName = nameof(ApiVersions.V1))]
        public ApiDataResult<PagingData<sys_topic>> getMemberInfoByPhone(int? topictypeId,string? topicName,int pageIndex = 1, int pageSize = 100)
        {
            //校验验证码是否可用，如果可用，返回true
            ApiDataResult<PagingData<sys_topic>> Res = _ITopicBLL.getTopicList(topictypeId, topicName, pageSize, pageIndex);
            return Res;
        }

      




    }

}
